package com.ruoyi.orderEat.controller;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.orderEat.domain.ShopCart;
import com.ruoyi.orderEat.domain.ShopInfo;
import com.ruoyi.orderEat.domain.ShopInfoVO;
import com.ruoyi.orderEat.service.IShopInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品信息Controller
 *
 * @author DingHao
 * @date 2023-11-22
 */
@RestController
@RequestMapping("/orderEat/shopInfo")
public class ShopInfoController extends BaseController
{
    @Autowired
    private IShopInfoService shopInfoService;

    /**
     * 查询商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('orderEat:shopInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShopInfo shopInfo)
    {
        startPage();
        PageInfo<ShopInfoVO> pageInfo = shopInfoService.selectShopInfoList(shopInfo);
        return  getDataTable(pageInfo.getList());
    }

    /**
     * 查询商品信息下拉选列表
     */
    @PreAuthorize("@ss.hasPermi('orderEat:shopInfo:list')")
    @GetMapping("/dropDownList")
    public AjaxResult dropDownList( )
    {
        PageInfo<ShopInfoVO> shopInfoVOPageInfo = shopInfoService.selectShopInfoList(new ShopInfo());
        List<ShopInfoVO> list = shopInfoVOPageInfo.getList();
        //List<ShopInfoVO> list = shopInfoService.selectShopInfoList(new ShopInfo());
        return success(list);
    }


    /**
     * 导出商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('orderEat:shopInfo:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopInfo shopInfo)
    {
        PageInfo<ShopInfoVO> shopInfoVOPageInfo = shopInfoService.selectShopInfoList(shopInfo);
        List<ShopInfoVO> list = shopInfoVOPageInfo.getList();
        ExcelUtil<ShopInfoVO> util = new ExcelUtil<ShopInfoVO>(ShopInfoVO.class);
        util.exportExcel(response, list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi({'orderEat:shopInfo:query','wx'})")
    @GetMapping(value = "/{shopId}")
    public AjaxResult getInfo(@PathVariable("shopId") Long shopId)
    {
        return success(shopInfoService.selectWxShopInfoByShopId(shopId));
    }

    /**
     * 新增商品信息
     */
    @PreAuthorize("@ss.hasPermi('orderEat:shopInfo:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopInfo shopInfo)
    {
        return toAjax(shopInfoService.insertShopInfo(shopInfo));
    }

    /**
     * 修改商品信息
     */
    @PreAuthorize("@ss.hasPermi('orderEat:shopInfo:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopInfo shopInfo)
    {
        return toAjax(shopInfoService.updateShopInfo(shopInfo));
    }

    /**
     * 删除商品信息
     */
    @PreAuthorize("@ss.hasPermi('orderEat:shopInfo:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shopIds}")
    public AjaxResult remove(@PathVariable Long[] shopIds)
    {
        return toAjax(shopInfoService.deleteShopInfoByShopIds(shopIds));
    }

    /**
     * 购物车添加(+1)
     */
    @PreAuthorize("@ss.hasPermi({'orderEat:shopInfo:add','wx'})")
    @Log(title = "购物车添加(+1)", businessType = BusinessType.INSERT)
    @PostMapping("/addCartShop")
    public AjaxResult addCartShop(@RequestBody ShopCart shopCart)
    {
        shopInfoService.addCart(shopCart);
        return success();
    }

    /**
     * 购物车添加(多数量)
     */
    @PreAuthorize("@ss.hasPermi({'orderEat:shopInfo:add','wx'})")
    @Log(title = "购物车添加(多数量)", businessType = BusinessType.INSERT)
    @PostMapping("/addCartShopBatch")
    public AjaxResult addCartShopBatch(@RequestBody ShopCart shopCart)
    {
        shopInfoService.saveCartShopBatch(shopCart);
        return success();
    }

    /**
     * 购物车减少
     */
    @PreAuthorize("@ss.hasPermi({'orderEat:shopInfo:remove','wx'})")
    @Log(title = "购物车减少", businessType = BusinessType.INSERT)
    @PostMapping("/delCartShop")
    public AjaxResult delCartShop(@RequestBody ShopCart shopCart)
    {
        shopInfoService.delCart(shopCart);
        return success();
    }

    /**
     * 清空购物车
     */
    @PreAuthorize("@ss.hasPermi({'orderEat:shopInfo:remove','wx'})")
    @Log(title = "清空购物车", businessType = BusinessType.INSERT)
    @PostMapping("/delAllCartShop/{tableId}")
    public AjaxResult delAllCartShop(@RequestBody Map<String,Object> shopCarts, @PathVariable Integer tableId)
    {
        List<?> list = (List<?>) shopCarts.get("shopCarts");
        List<ShopCart> shopCartList = list.stream().map(item -> JSON.parseObject(JSON.toJSONString(item), ShopCart.class)).collect(Collectors.toList());
        shopInfoService.delAllCart(shopCartList,tableId);
        return success();
    }

    /**
     * 查询购物车列表
     */
    @PreAuthorize("@ss.hasPermi({'orderEat:shopInfo:list','wx'})")
    @GetMapping("/cartList/{tableId}")
    public AjaxResult cartList(@PathVariable Integer tableId)
    {
        List<ShopCart> shopCartList = shopInfoService.shopCartList(tableId);
        return success(shopCartList);
    }

    /**
     * 查询购物车总价
     */
    @PreAuthorize("@ss.hasPermi({'orderEat:shopInfo:list','wx'})")
    @GetMapping("/cartAmount/{tableId}")
    public AjaxResult cartAmount(@PathVariable Integer tableId)
    {
        String allSale = shopInfoService.saleAmountCount(tableId);
        return success(allSale);
    }

    /**
     * 查询购物车总商品数量
     */
    @PreAuthorize("@ss.hasPermi({'orderEat:shopInfo:list','wx'})")
    @GetMapping("/cartNumCount/{tableId}")
    public AjaxResult cartNumCount(@PathVariable Integer tableId)
    {
        String cartCount = shopInfoService.getCartCount(tableId);
        return success(cartCount);
    }



}
